package com.qfoud.edu.aunt.commons.controlller;

import com.qfoud.edu.aunt.commons.pojo.User;
import com.qfoud.edu.aunt.commons.service.IUserService;
import com.qfoud.edu.aunt.commons.service.impl.UserService;
import com.qfoud.edu.framwork.annotation.Controller;
import com.qfoud.edu.framwork.annotation.RequestMapping;
import com.qfoud.edu.framwork.pojo.Model;
import com.qfoud.edu.shiro.framwork.annotation.HasPerms;
import com.qfoud.edu.shiro.framwork.annotation.HasRoles;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xiaobobo
 * @title: UserController
 * @projectName cd-java-2303-project-parent
 * @description: TODO
 * @date 2023/5/11  9:08
 */
@Controller
@RequestMapping("/users")
public class UserController {

    private IUserService userService=new UserService();

    /**
     * 假设这个方法 需要 (user:list这个权限才能访问)
     * @param model
     * @return
     * @throws Exception
     */
    @HasPerms(perms = {"user:list"})
    @RequestMapping("/list.action")
    public String list(Model model) throws Exception {
        List<User> userList=userService.findUserAll();
        model.setAttribute("userList",userList);
        return "/user_list.jsp";
    }

    /**
     * 根据id删除数据
     *  假设这个方法需要 admin 角色才能访问
     *
     * @param id
     * @return
     */
    @HasRoles(roles = {"admin"})
    @RequestMapping("/delete.action")
    public String delete(Integer id) throws Exception {
        userService.deleteById(id);
        return "/users/list.action";
    }

    /**
     * 通过id找用户对象
     * @param id
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/findUserById.action")
    public String findUserById(Integer id,Model model) throws Exception {
        User user=userService.findUserById(id);
        model.setAttribute("user",user);
        return "/user_edit.jsp";
    }


    /**
     * 通过id修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateById.action")
    public String updateById(User user) throws Exception {
        userService.updateById(user);
        return "/users/list.action";
    }

    /**
     * 添加用户的方法
     * @param user
     * @return
     */
    @RequestMapping("/add.action")
    public String add(User user) throws Exception {

        userService.add(user);

        return "/users/list.action";
    }




}
